package tech.yunland.shop.web;

import com.moyuer.bsf.domain.model.User;
import com.moyuer.bsf.common.web.AuthNotRequired;
import com.moyuer.bsf.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tech.yunland.shop.domain.service.SchedulerJobService;

/**
 * 任务控制器
 *
 * @author 文卡<wkwenka@gmail.com>  on 17-5-24.
 */
@RestController
@RequestMapping("/scheduler")
public class SchedulerJobController extends BaseController{

    @Autowired
    private SchedulerJobService schedulerJobService;

    /**
     * 用户试用期更新
     *
     * @param account
     * @param password
     * @param days
     */
    @AuthNotRequired
    @RequestMapping(value = "/renewalFee", method = RequestMethod.POST)
    public void update(@RequestParam(required = true) String account,
                       @RequestParam(required = true) String password,
                       @RequestParam(required = true) Integer days) {

        User user = new User();
        user.setAccount(account);
        user.setPassword(password);
        try {
            this.schedulerJobService.updateSchedulerJob(user, days);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    /**
     * 获取用户试用期剩余天数
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public String getValidPeriod(@RequestParam(required = false) String userId) {
        if (StringUtils.isBlank(userId)){
            userId = this.currentUserId;
        }
        String validPeriod = this.schedulerJobService.updateOrGetValidPeriod(userId);
        return validPeriod;
    }

}
